package com.hhit.bigscreenbackmanage.controller.pdf;

import com.alibaba.fastjson.JSONObject;
import com.hhit.bigscreenbackmanage.common.PageUtil;
import com.hhit.bigscreenbackmanage.entity.pdf.PdfMlTree;
import com.hhit.bigscreenbackmanage.entity.pdf.Pdfml;
import com.hhit.bigscreenbackmanage.entity.pdf.Pdftz;
import com.hhit.bigscreenbackmanage.service.pdf.PdfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/PdfController")
public class PdfController {

    @Autowired
    private PdfService pdfService;

    /**
     * 获取目录
     *
     * @param
     * @return list
     */
    @RequestMapping("/getPdfml")
    public List<PdfMlTree> getPdfml(@RequestParam Map map) {
        try {
            List<PdfMlTree> list = pdfService.getPdfml(map);
            return  list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取图纸
     *
     * @param
     * @return list
     */
    @RequestMapping("/getPdftz")
    public PageUtil<Pdftz> getPdftz(@RequestParam Map map) {
        try {
            PageUtil<Pdftz> list = pdfService.getPdftz(map);
            return  list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/getBase64DataInfo")
    public JSONObject getBase64DataInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {

        Map<String, String> paramMap = new HashMap<String, String>();
        String[] params = new String[]{
                "UUID", "WJMC"
        };
        for (String param : params) {
            paramMap.put(param, request.getParameter(param));
        }

        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");

        String uuid = paramMap.get("UUID").replaceAll("\\\\", "/").replaceAll("limrlimrlimr","#").replaceAll("zhangmaizhangmaizhangmai","^");
        String wjmc = paramMap.get("WJMC").replaceAll("limrlimrlimr","#").replaceAll("zhangmaizhangmaizhangmai","^");

        response.setContentType("application/pdf");
//        uuid = uuid.substring(0, uuid.lastIndexOf(".")) + ".pdf";

        File file = new File(uuid);
        OutputStream os = null;
        FileInputStream bis = null;
        byte[] buff = new byte[1024];

        try {
            if (file.exists()) {
                os = response.getOutputStream();
                bis = new FileInputStream(file);
                int i = 0;
                while ((i = bis.read(buff)) != -1) {
                    os.write(buff, 0, i);
                    os.flush();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                bis.close();
            }
        }

        return null;
    }

    @RequestMapping("/getFile")
    public void getFile(@RequestParam String path) {
        System.out.println("文件导入开始");
        getAllFiles(new File(path),0);
        System.out.println("文件导入结束");
        return;
    }

    public void getAllFiles(File dir,int level){
        if(level>0){
            if(level == 1){
                Pdfml pdfml = new Pdfml();
                pdfml.setMlbm(dir.toString().replaceAll("\\\\","/"));
                pdfml.setMlmc(dir.getName());
                pdfml.setSjmlbm(dir.toString().replaceAll("\\\\","/"));
                pdfml.setCj(""+level);
                if(pdfService.findPdfml(pdfml) == 0){
                    pdfService.addPdfml(pdfml);
                }
            }else{
                Pdfml pdfml = new Pdfml();
                pdfml.setMlbm(dir.toString().replaceAll("\\\\","/"));
                pdfml.setMlmc(dir.getName());
                pdfml.setSjmlbm(dir.getParent().replaceAll("\\\\","/"));
                pdfml.setCj(""+level);
                if(pdfService.findPdfml(pdfml) == 0){
                    pdfService.addPdfml(pdfml);
                }
            }
        }
        level++;
        File[] files=dir.listFiles();
        for(int i=0;i<files.length;i++)
        {
            if(files[i].isDirectory())
            {
                //这里面用了递归的算法
                getAllFiles(files[i],level);
            }
            else {
                Pdftz pdftz = new Pdftz();
                pdftz.setTzmc(files[i].getName());
                pdftz.setSsmlbm(files[i].toString().replaceAll("\\\\","/"));
                pdftz.setLj(files[i].toString().replaceAll("\\\\","/"));
                if(pdfService.findPdftz(pdftz) == 0){
                    pdfService.addPdftz(pdftz);
                }
            }
        }
    }
}
